package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.TestVideo;
import com.newway.back.mapper.TestVideoMapper;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/system/testVideo")
public class TestVideoController {

    @Resource
    TestVideoMapper testVideoMapper;

    @GetMapping("/list")
    public Result getList(TestVideo testVideo){
        QueryWrapper<TestVideo> testVideoQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(testVideo.getTitle())){
            testVideoQueryWrapper.like("title",testVideo.getTitle());
        }
        if(StringUtils.isNotBlank(testVideo.getDifficulty())){
            testVideoQueryWrapper.like("difficulty",testVideo.getDifficulty());
        }
        if(StringUtils.isNotBlank(testVideo.getTestType())){
            testVideoQueryWrapper.like("test_type",testVideo.getTestType());
        }
        Page<TestVideo> testVideoPage = new Page<>(testVideo.getPageNum(), testVideo.getPageSize());
        Page<TestVideo> result = testVideoMapper.selectPage(testVideoPage, testVideoQueryWrapper);
        return Result.success(result);
    }

    @PostMapping("/addOrUpdate")
    public Result add(@RequestBody TestVideo testVideo){
        if(null == testVideo.getId()){
            testVideo.setCreateTime(LocalDateTime.now());
        }
        testVideoMapper.insertOrUpdate(testVideo);
        return Result.success();
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        testVideoMapper.deleteById(id);
        return Result.success();
    }
}
